package program;

import java.io.File;
import java.util.List;

import kernel.FileManager;
import kernel.exception.OSException;
import kernel.process.AbstractProcess;
import kernel.process.AbstractProcessHeader;

/**
 * Mkdir/Md process.
 * For more see man() method.
 * @author Jaromir Lepic, Veronika Vencurikova
 *
 */
public class Mkdir extends AbstractProcess {
	public static final String CREATE_SUBDIRS = "-p";
	public static final String VERBOSE = "-v";
	private boolean subdirs = false;
	private boolean verbose = false;
	private int parIndex = 0;

	public Mkdir(AbstractProcessHeader header, long pid) {
		super(header, pid);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String doJob(String input) throws OSException {
		for (String param: params){
			if (param.toLowerCase().equals(CREATE_SUBDIRS)) {
				subdirs = true;
				parIndex++;
			} else if (param.toLowerCase().equals(VERBOSE)){
				verbose = true;
				parIndex++;
			}
		}
		File newFile = absPath(this.params.get(parIndex), shellID);
		if (subdirs){
			List<String> names = FileManager.createDirWithSubdirs(newFile);
			if (verbose){
				String returnVal = "Created directories:\n";
				for (String name: names){
					returnVal += "\t" + name + "\n";
				}
				return returnVal;
			} else {
				return "";
			}
		}
		else {
			String name = FileManager.createDir(newFile);
			if (verbose){
				return "Created directory:\n\t"+name;
			} else {
				return "";
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean needParams() {
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void kill() throws OSException {
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String continueJob() throws OSException {
		this.shouldEnd = true;
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected String finalizeJob() throws OSException {
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String man(String command) {
	String man = "";
		
		man += "    makes directories\n";
		man += "\n";
		man += "Command syntax:\n";
		man += "    "+command+" [OPTION] DIRECTORY\n";
		man += "\n";
		man += "Description:\n";
		man += "    -p - makes parent directories as needed\n";
		man += "    -v - displays for each created directory\n";
		man += "\n";
		man += "Examples:\n";
		man += "    "+command+" -p pictures\\flowers\n";
		man += "    "+command+" downloads\n";
		
		return man;
	}

}
